<?php
if(!defined('IN_WSHUO')) exit();

function is_email($string){
	return preg_match('/^[A-z0-9._-]+@[A-z0-9._-]+\.[A-z0-9._-]+$/', $string);
}

function is_name($string){
	return preg_match('/^[\w\x80-\xff]{3,15}$/', $string);
}

function daddslashes($str){
	return (!get_magic_quotes_gpc())?addslashes($str):$str;
}

function userid($username,$key,$mpwd){
	return md5($username.$key.$mpwd);
}


function cpost($url,$data){
	$ch=curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_USERAGENT,'MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1');
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch,CURLOPT_POST,1);
	curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
	curl_setopt($ch, CURLOPT_TIMEOUT, 20);
	$result=curl_exec($ch);
	curl_close($ch);
	return $result;
}



function cget($url){
	$ch=curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_USERAGENT,'MQQBrowser/26 Mozilla/5.0 (Linux; U; Android 2.3.7; zh-cn; MB200 Build/GRJ22; CyanogenMod-7) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1');
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	$result=curl_exec($ch);
	curl_close($ch);
	return $result;
}

function getsid($str){

	preg_match('/<a.*href=("|\').*sid=(.*)\&.*>.*<\/a>/iU',$str,$sid);
	return $sid;
}

function time_data($time){
	return date('Y-m-d H:i:s',$time);
}

function csid($qq,$sid){
	$url='http://ish.z.qq.com/infocenter_v2.jsp?B_UID='.$qq.'&sid='.$sid.'&g_ut=1';
	$ch=curl_init();
	curl_setopt($ch,CURLOPT_URL,$url);
	curl_setopt($ch,CURLOPT_USERAGENT,'TTMobile/09.03.18/symbianOS9.1 Series60/3.0 Nokia6120cAP3.03');
	curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
	$result=curl_exec($ch);
	curl_close($ch);
	if(strstr($result,'个人中心')){
		return '正常运行';
	}else{
		return '<small style="color:red">SID失效</small>';
	}
}

function on_off($do,$a,$qq,$id){
	$on='已开启<a href="index.php?action=off&d='.$a.'&qq='.$qq.'&id='.$id.'"><span class="label label-default">点此关闭</span></a>';
	$off='已关闭 <a href="index.php?action=on&d='.$a.'&qq='.$qq.'&id='.$id.'"><span class="label label-primary">点此开启</span></a>';
	$result=$do?$on:$off;
	return $result;
}
function cont($do){
	$result=explode("\n",file_get_contents("../cron/content/".$do.".txt"));
	$i=array_rand($result);
	return $result[$i];
}

function cimg($do){
	$result=glob("../cron/image/".$do."*.*");
	$i=array_rand($result);
	return $result[$i];
}
function w_ubb($ubb){
	$time=date("Y-m-d H:i:s");
	$ubb=preg_replace('/\[随机\]/', cont("sj"), $ubb);
	$ubb=preg_replace('/\[语录\]/', cont("yl"), $ubb);
	$ubb=preg_replace('/\[伤感语录\]/', cont("sgyl"), $ubb);
	$ubb=preg_replace('/\[社会语录\]/',cont("shyl"), $ubb);
	$ubb=preg_replace('/\[时间\]/', $time, $ubb);
	return $ubb;
}
function m_ubb($ubb){
	$ubb=preg_replace('/\[美女\]/', cimg("mm_"), $ubb);
	$ubb=preg_replace('/\[动漫\]/', cimg("dm_"), $ubb);
	$ubb=preg_replace('/\[随机图\]/', cimg(""), $ubb);
	$ubb=preg_replace('/\[伤感图\]/', cimg("sg_"), $ubb);
	return $ubb;
}

function get_ip(){
	if(!empty($_SERVER["HTTP_CLIENT_IP"])){
	  $cip = $_SERVER["HTTP_CLIENT_IP"];
	}
	elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
	  $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
	}
	elseif(!empty($_SERVER["REMOTE_ADDR"])){
	  $cip = $_SERVER["REMOTE_ADDR"];
	}
	else{
	  $cip = "无法获取！";
	}
	return $cip;
}